﻿/**
 * 插入图片插件
 * 增加输入图片来源，仅供词条页面使用
 */
(function(E){
var Consts = E.consts, HdImage = 'HdImage', Event = E.mod('Event'),
	//static_url = (typeof STATIC_URL === 'string') ? STATIC_URL+'/js/jqeditor' : './js/jqeditor';
	//jqe_static_url全局变量，功外部修改
	static_url = typeof jqe_static_url === 'string' ? jqe_static_url : './';

Event.bind("oncomplete", function(){
	setTimeout(function(){
		resizeImageDivWidth();
	}, 500);

	setTimeout(function(){
		resizeImageDivWidth();
	}, 10000);
});

// 修正内容里面的图片外面div的width可能错误的问题
// 如图片宽300，但是div的width=80px
function resizeImageDivWidth( ){
	var div, config = E.plugin(HdImage).config;

	E.$('img').each(function(i, el){
		el.removeAttribute('height');
		el.removeAttribute('style');

		div = E.$(el).closest('div.img');
		// 如果图片的宽度大于
		if( el.width > config.max_width ){
			el.setAttribute('width', config.max_width);
		}

		if (div.size() ){
			if( el.width && el.width > 80 ){
				div.width(el.width);
				div.css('height', '');
			}
			div.find('strong').css('width', '');
		}
	});
}

E.plugin(HdImage, {
	inserthtml:'custom',

	config : {
		width:500,
		max_width : 600,
		position: 'center',
		overlay: true
	},
	//可供外部修改的配置信息
	settings : {
		//是否显示3列大小选项
		moreSize : true,
		maxUploadSize : '5M',
		//是否显示3列位置选项
		moreAlign : true,
		//上传图片按钮和添加url按钮被选中样式
		selectedClass : "selectedClass",
		//上传图片相关参数，默认使用小百科的
		img_src_action : 'baike_article_create.php',
		//图片描述是否可以为空
		discription_can_null : false,
		//获取上传input图片表单提交地址
		//[必须外部重写该方法]
		//[返回数据格式如{action:'www.abc.com', postParms:{name:'img'}}，
		//如果没有需要post提交的参数则可以直接返回string类型的url]
		getFileAction : function(){return alert('没有提供可调用的上传接口');},
		//获取上传url图片表单提交地址[必须外部重写该方法][参数同file]
		getUrlAction : function(){return alert('没有提供可调用的上传接口');}
	},
	//插件使用的一些静态属性配置
	staticAttr : {
		//file表单元素的name属性[该值不能修改,否则无法上传]
		inputFileName : "photofile",
		//url输入框
		inputUrlName : "img_url",
		//上传图片按钮name属性值
		uploadFileBtn : "uploadFileBtn",
		//添加url按钮name属性值
		urlFileBtn : "urlFileBtn",
		//上传图片显示标签name属性值
		uploadShowName : "uploadShowName",
		//添加url图片显示标签属name性值
		urlShowName : "urlShowName",
		//图片大小单选按钮name属性值
		imgSizeName : "picWidthHeight",
		//图片显示方式单选按钮name属性值
		imgAlignName : "picAlign",
		//图片描述name属性值
		imgDiscription : "picAlt",
		//提交按钮name属性值
		uploadButton : "uploadButton",
		//上传功能单例运行
		uploadIsBusy : false
	},
	style : '.jqe-plugin-HdImage{width:90%;margin:0 auto;}\
			.jqe-plugin-HdImage .input_tr{height:26px;}\
			.jqe-plugin-HdImage .first_span{left:15px;}\
			.jqe-plugin-HdImage .last_span{left:110px;}\
			.jqe-plugin-HdImage .uploadBoxTop{position:relative;border-bottom:1px solid #009AD9;height:29px;margin:0 0 3px;padding:0;}\
			.jqe-plugin-HdImage .uploadBoxTop span{position:absolute;top:0px;color:#666666;cursor:pointer;display:inline-block;line-height:25px;text-align:center;width:80px;border-left: 1px solid #FFFFFF;border-right: 1px solid #FFFFFF;border-top: 4px solid #FFFFFF;}\
			.jqe-plugin-HdImage .uploadBoxTop span.selectedClass{background-color:#FFFFFF;cursor:default;border-left: 1px solid #009AD9;border-right: 1px solid #009AD9;border-top: 4px solid #009AD9;border-bottom:2px solid #FFFFFF;color:#333333;}\
			.jqe-plugin-HdImage input,textarea{color:#111111;font-family:"lucida grande",taho,verdana,"trebuchet ms",sans-serif;font-size:12px;}\
			.jqe-plugin-HdImage .inputFile{height:22px;width:260px;border:1px solid #7F9DB9;}\
			.jqe-plugin-HdImage .inputUrl{height:18px;line-height:18px;width:410px;border:1px solid #7F9DB9;}\
			.jqe-plugin-HdImage .tips{color:#999999;line-height:22px;text-align:left;wodr-wrap:break-word;}\
			.jqe-plugin-HdImage .bgImg{height:61px;background:url("'+static_url+'/skins/bg/uploadimg_bg.gif") no-repeat scroll 0 0 transparent;margin:0 0 5px 10px;border:1px solid #999999;}\
			.jqe-plugin-HdImage .bgImg1{background-position:0 0px;width:70px;}\
			.jqe-plugin-HdImage .bgImg2{background-position:-72px -0px;width:68px;}\
			.jqe-plugin-HdImage .bgImg3{background-position:-142px 0;width:69px;}\
			.jqe-plugin-HdImage .alignImg{background:url("'+static_url+'/skins/bg/uploadimg_bg.gif") no-repeat scroll 0 0 transparent;margin:0 0 5px 10px;height:46px;border:1px solid #999999;}\
			.jqe-plugin-HdImage .alignImg1{background-position:0 -69px;width:70px}\
			.jqe-plugin-HdImage .alignImg2{background-position:-71px -69px;width:68px;}\
			.jqe-plugin-HdImage .alignImg3{background-position:-142px -69px;width:69px;}\
			.jqe-plugin-HdImage strong{line-height:22px;}\
			.bgpicboxOn{ border-color:#98C6E4; background:#CAE5F8;}\
			.jqe-plugin-HdImage{width:90%;margin:0 auto;}\
		.jqe-plugin-HdImage .input_tr{height:26px;}\
		.jqe-plugin-HdImage .first_span{left:15px;}\
		.jqe-plugin-HdImage .last_span{left:110px;}\
		.jqe-plugin-HdImage .uploadBoxTop{position:relative;border-bottom:1px solid #009AD9;height:29px;margin:0 0 3px;padding:0;}\
		.jqe-plugin-HdImage .bgImg{height:35px;background:url("http://www.huimg.cn/jqeditor/skins/bg/uploadimg_bg.png") no-repeat scroll 0 0 transparent;margin:0 0 5px 10px;border:1px solid #999999;}\
		.jqe-plugin-HdImage .bgImg1{background-position:0 0px;width:55px;}\
		.jqe-plugin-HdImage .bgImg2{background-position:-72px -0px;width:68px;}\
		.jqe-plugin-HdImage .bgImg3{background-position:-55px 0;width:55px;}\
		.jqe-plugin-HdImage .alignImg{background:url("http://www.huimg.cn/jqeditor/skins/bg/uploadimg_bg.png") no-repeat scroll 0 0 transparent;margin:0 0 5px 10px;height:35px;border:1px solid #999999;}\
		.jqe-plugin-HdImage .alignImg1{background-position:0 -35px;width:55px}\
		.jqe-plugin-HdImage .alignImg2{background-position:-71px -69px;width:68px;}\
		.jqe-plugin-HdImage .alignImg3{background-position:-55px -35px;width:55px;}\
		.jqe-plugin-HdImage strong{line-height:22px;}\
		.jqe-plugin-Reference{width:500px;overflow-x:hidden;font:12px/1.2 宋体,Arial,Helvetica,sans-serif;background:none repeat scroll 0 0 #FFFFFF;}\
		.jqe-plugin-Reference a{color:#0268CD;text-decoration:none;}\
		.jqe-plugin-Reference p{margin:0;padding:0;}\
		.jqe-plugin-Reference a:hover{text-decoration:underline;}\
		.jqe-plugin-Reference #referlist{width:490px;height:250px;overflow-y:scroll;overflow-x:hidden;border:1px solid #CCCCCC;line-height:18px;padding:5px;margin:0;}\
		.jqe-plugin-Reference #referlist li{padding:10px 0 10px 25px;list-style:none;display:inline-block;width:450px;}\
		.jqe-plugin-Reference .refer_item input{margin:0px 0px 0px -20px;float:left;height:18px;}\
		.jqe-plugin-Reference .refer_item a{float:right;margin-left:5px;margin-right:5px;}\
		.jqe-plugin-Reference .refer_item span.title{width:360px;float:left;}\
		.jqe-plugin-Reference .refer_item span.url{float:none;display:block;}\
		.jqe-plugin-Reference .refer_item p{display:inline-block;}\
		.jqe-plugin-Reference .gray{color:#999999;}\
		.jqe-plugin-Reference p.top{padding:10px 5px;margin-bottom:10px;}\
		.jqe-plugin-Reference .top a{float:right;}\
		.jqe-plugin-Reference .top span{float:left;}\
		.jqe-plugin-Reference #editor_mod{background:none repeat scroll 0 0 #F8F8F8;}\
		.jqe-plugin-Reference #editor_mod .editor_p{padding-bottom:10px;display:block;}\
		.jqe-plugin-Reference #editor_mod a{margin-left:10px;}\
		.jqe-plugin-Reference #editor_mod input{border:1px solid #7F9DB9;padding:2px 6px;height:15px;color:#111111;}\
	#editform li.jqe-button{margin:4px 1px 2px;}\
	#editform li.jqe-button span{margin:0;width:22px;}\
	#editform li.jqe-button span.selected-color{width:18px;}\
	.bgpicbox{ display:inline-block; width:71px; height:61px; padding-top:7px; border:1px solid #FFF; text-align:center;}\
	.bgpicbox div{ margin-left:7px !important;}\
	.jqe-plugin-HdImage .alignImg4{background-position:-110px 0;width:55px;}\
	.f14{font-size: 14px;}',
	init : function() {
		var self = this,
			staticAttr = self.staticAttr,
			settings = self.settings,
			moreSize = settings.moreSize,
			moreAlign = settings.moreAlign,
			img_src_action = settings.img_src_action,
			discription_can_null = settings.discription_can_null;
		var html = '<table class="jqe-plugin-HdImage">\
				<tbody>\
					<tr>\
						<td>\
							<div class="uploadBoxTop">\
								<span class="'+settings.selectedClass+' first_span" name="'+staticAttr.uploadFileBtn+'">\
									上传图片\
								</span>\
								<span name="urlFileBtn" class="last_span">\
									添加url\
								</span>\
							</div>\
						</td>\
					</tr>\
					<tr class="input_tr" name="' + staticAttr.uploadShowName + '">\
						<td>\
							<input type="file" size="60" name="' + staticAttr.inputFileName + '" class="inputFile" style="width:410px;">\
							<input type="hidden" name="img_src_action" value="'+img_src_action+'">\
						</td>\
					</tr>\
					<tr class="input_tr" name="' + staticAttr.uploadShowName + '">\
						<td>\
							<label class="tips">图片格式：jpg、jpeg、png、gif，大小为'+settings.maxUploadSize+'以内</label>\
						</td>\
					</tr>\
					<tr class="input_tr" style="display:none;" name="' + staticAttr.urlShowName + '">\
						<td>\
							<input type="text" name="img_url" class="inputUrl">\
						</td>\
					</tr>\
					<tr class="input_tr" style="display:none;" name="' + staticAttr.urlShowName + '">\
						<td>\
							<label class="tips">注:请输入以jpg、jpeg、png、gif结束的url</label>\
						</td>\
					</tr>\
					<tr>\
						<td>\
							<table>\
								<tbody>\
									<tr>\
										<td width="97" valign="top" style="text-align:right;">\
											<strong class="f14">图片大小：</strong>\
										</td>\
										<td style="text-align:left;" width="114">\
											<label class="bgpicbox bgpicboxOn imgsize" imgsize="_s" value="1">\
												<div class="bgImg bgImg1" >\
												</div>大(宽300px)</label>\
												<input type="hidden" imgsize="_s" value="1" name="' + staticAttr.imgSizeName + '" >\
										</td>\
										<td style="text-align:left;" width="114">\
											<label class="bgpicbox imgsize"  imgsize="_140" value="2">\
												<div class="bgImg bgImg3">\
												</div>\
												小(宽140px)\
											</label>\
										</td>\
										<td>&nbsp;</td>\
									</tr>\
									<tr>\
										<td valign="top" style="text-align:right;">\
											<strong class="f14">\
												对齐方式：\
											</strong>\
										</td>\
										<td style="text-align:left;">\
											<label class="bgpicbox alignImage" value="left">\
												<div class="alignImg alignImg1">\
												</div>\
												<input type="hidden" value="right" name="' + staticAttr.imgAlignName + '" >图片居左\
											</label>\
										</td>\
										<td style="text-align:left;"><label class="bgpicbox alignImage" value="center"><div class="alignImg alignImg4"></div>图片居中</label></td>\
										<td style="text-align:left;">\
											<label class="bgpicbox alignImage bgpicboxOn" value="right">\
												<div class="alignImg alignImg3">\
												</div>\
												图片居右\
											</label>\
										</td>\
									</tr>\
								</tbody>\
							</table>\
						</td>\
					</tr>\
					<tr>\
					</tr>\
				</tbody>\
			</table>\
			<ul class="picly">\
				<li class="clearfix">\
					<span>\
						<i>*</i>图片描述：\
					</span>\
					<textarea name="picAlt" class="desc"></textarea>\
				</li>\
				<li class="clearfix">\
					<span >图片来源文章：</span>\
					<input type="text" name="title" value="请注明可靠来源供读者查证" defaultvalue="请注明可靠来源供读者查证"/>\
					<!--鼠标点击input时增加样式:cdgray-->\
				</li>\
				<li class="clearfix">\
					<span>文章URL：</span>\
					<input type="text" name="url" value="http://" class="" defaultvalue="http://"/>\
					<!--鼠标点击input时增加样式:cdgray-->\
				</li>\
				<li class="clearfix">\
					<span>来源网站名：</span>\
					<input name="site" type="text" />\
				</li>\
			</ul>';
		self.cache['html'] = html;
	},
	/*
		额外绑定事件
		target : 事件源 jquery对象
		panel : 面板
		self ： 插件本身
	*/
	action : function( target, panel, self ) {
		var fun = target.attr("name");
		if(typeof self[fun] === Consts.typeof_function) {
			return self[fun](target, panel, self);
		}
	},
	callback : function( panel, self ) {
		var staticAttr = self.staticAttr;
		panel.find('.alignImage').live ('click',function () {
			var self = $(this),
				value = self.attr('value'),
				index =panel.find('.alignImage').index(this);
			panel.find('.alignImage').removeClass('bgpicboxOn').eq(index).addClass('bgpicboxOn')
			panel.find("[name=" + staticAttr.imgAlignName + "]").val(value);
		});
		panel.find('.imgsize').live ('click',function () {
			var self = $(this),
				Imgsize = self.attr('Imgsize'),
				value = self.attr('value'),
				index =panel.find('.imgsize').index(this);
			panel.find('.imgsize').removeClass('bgpicboxOn').eq(index).addClass('bgpicboxOn')
			panel.find("[name=" + staticAttr.imgSizeName+"]").attr('imgsize',Imgsize).val(value);
		});
		panel.find("input[name=title],input[name=site],input[name=url],.desc").live('focus',
			function(){
				$(this).addClass('cdgray');
				if($.inArray($(this).attr('name'),['url','title'])>-1 && $(this).val() == $(this).attr('defaultvalue')){
					$(this).val('')
				}
			}).live('blur',
			function(){
				$(this).removeClass('cdgray');
				if(($(this).attr('name') =='url' || $(this).attr('name') =='title') && $.trim($(this).val()) == ''){
					$(this).val($(this).attr('defaultvalue') )
				}
			});
		panel.find(":submit").replaceWith("<input type='button' value=' 提 交 ' name='" + staticAttr.uploadButton + "'>");
		panel.find('.desc').focus();
	},
	/*
		单击上传图片按钮触发事件
		target : 事件源 jquery对象
		panel : 面板
		self ： 插件本身
	*/
	uploadFileBtn : function( target, panel, self ) {
		var staticAttr = self.staticAttr,
			settings = self.settings;
		panel.find("[name=" + staticAttr.uploadShowName+"]").show();
		panel.find("[name=" + staticAttr.urlShowName+"]").hide();
		panel.find("[name=" + staticAttr.urlFileBtn+"]").removeClass(settings.selectedClass);
		target.addClass(settings.selectedClass);
		return false;
	},
	/*
		单击添加url按钮触发事件
		target : 事件源 jquery对象
		panel : 面板
		self ： 插件本身
	*/
	urlFileBtn : function( target, panel, self ) {
		var staticAttr = self.staticAttr,
			settings = self.settings;
		panel.find("[name=" + staticAttr.uploadShowName + "]").hide();
		panel.find("[name=" + staticAttr.urlShowName + "]").show();
		panel.find("[name=" + staticAttr.uploadFileBtn + "]").removeClass(settings.selectedClass);
		target.addClass(settings.selectedClass);
		return false;
	},
	ok : function( panel, self ) {
		/*
			这里只为了显示出按钮出来
		*/
	},
	onclose : function( panel, self ) {
		var staticAttr = self.staticAttr;
		staticAttr.uploadIsBusy = false;
	},
	/*
		确定插入图片
	*/
	uploadButton : function( target, panel, self ) {
		var staticAttr = self.staticAttr,
			settings = self.settings,
			//图片描述是否可以为空
			discription_can_null = settings.discription_can_null,
			//需要上传的文件
			obj = panel.find("[name="+staticAttr.inputFileName+"]"),
			//图片地址
			imgUrl = $.trim(panel.find("[name="+staticAttr.inputUrlName+"]").val()),
			//图片描述
			discription = $.trim(panel.find("[name="+staticAttr.imgDiscription+"]").val()),
			site = $.trim(panel.find("[name=site]").val()),
			title = $.trim(panel.find("[name=title]").val()),
			url = $.trim(panel.find("[name=url]").val()),
			//图片地址校验正则
			reg = /^(http\:\/\/)[\w.\/-]+\.(jpg|jpeg|gif|png)/i , // /^(\w|\.|\/)+\.(jpg|jpeg|gif|png)$/i;
			uploadFileBtn = panel.find("[name="+staticAttr.uploadFileBtn+"]"),
			//判断是上传图片还是添加url
			fig = uploadFileBtn.attr("class").indexOf(settings.selectedClass) > -1;
			url = url == 'http://' ? '' : url;
			title = title == '请注明可靠来源供读者查证' ? '' : title;
		//数据合法性校验
		if(fig && !self._checkFile(obj)) {
			self.error("提示：请选择一个有效的图片文件！");
			return false;
		}else if( !fig ) {
			if(imgUrl.length < 1) {
				self.error("图片url地址不能为空!");
				return false;
			}else if( !reg.test(imgUrl) ) {
				self.error("图片url地址格式有误!");
				return false;
			}
		}
		if( discription.length < 1 && !discription_can_null) {
			self.error("请填写图片描述，良好的描述可以帮助读者理解图片！");
			return false;
		}else if( discription.length > 500 ) {
			self.error("过多的图片描述可能会影响页面布局！");
			return false;
		}
		if (url != '' && !/^https?:\/\/([\w-]+\.)+[\w-]+(\/[\w.\/?%&= -]*)?/i.test(url)){
			self.error ('请输入一个有效的文章URL');
			return false;
		}
		if ( (url!='' || site != '') && title == '' ){
			self.error ('图片来源文章也要填哦');
			return false;
		}
		self._uploadFile();

		self.lock();
		setTimeout(function(){self.unlock()}, 10000);
	},
	//上传图片具体实现[内部调用方法]
	_uploadFile : function() {
		var self = E.plugin(HdImage),
			staticAttr = self.staticAttr,
			//iframe的id和name属性值
			iframefig = "articleUpload_iframe";
		if(staticAttr.uploadIsBusy) {
			self.error("请稍后...");
			return;
		}else {
			staticAttr.uploadIsBusy = true;
		}
		//iframe
		var iframe = '<iframe id="'+iframefig+'" name="'+iframefig+'" width="0" height="0"></iframe>';
		if(!$("#"+iframefig).length){
			$("body").append(iframe);
		}
		//提交
		self._uploadForm(iframefig);
	},
	//构造上传图片地址和form表单相关属性值
	_uploadForm : function( target ) {
		var self = E.plugin(HdImage),
			panel = self.panel,
			staticAttr = self.staticAttr,
			settings = self.settings,
			uploadFileBtn = panel.find("[name="+staticAttr.uploadFileBtn+"]"),
			//form表单
			form = panel.find("form"),
			//获取配置信息
			settings = self.settings,
			//form相关参数设置
			formAttrs = {"target" :target, "method" : "POST", "onsubmit" : "return true"},
			action = null,
			parms = null,
			formtype = 'enctype';
		//为了兼容不同版本jq对encoding兼容性处理不同，这里对IE6,7做特殊处理
		// 对于ie8+使用ie7标准的情况，需要使用document.documentMode来判断
		if($.browser.msie && ($.browser.version < 8 || document.documentMode ==7 )){
			formtype = 'encoding';
		}
		//选中的上传图片
		if(uploadFileBtn.hasClass(settings.selectedClass)) {
			action = settings.getFileAction();
			if(!action) {
				staticAttr.uploadIsBusy = false;
				return false;
			}
			//兼容不同版本的jquery，采用原生的设置
			form[0].setAttribute(formtype,'multipart/form-data');
		}else {
			action = settings.getUrlAction();
			if(!action) {
				staticAttr.uploadIsBusy = false;
				return false;
			}

			form[0].setAttribute(formtype,'application/x-www-form-urlencoded');
		}
		if(typeof action === 'string'){
			formAttrs['action'] = action;
		}else if(typeof action === 'object') {
			formAttrs['action'] = action.action;
			if(typeof action.postParms === 'object') {
				parms = action.postParms;
				for(var k in parms) {
					form.append('<input type="hidden" name="'+k+'" value="'+parms[k]+'">');
				}
			}
		}
		form.attr(formAttrs);
		//显示loading开始上传
		self.error("图片上传中....");
		//兼容不同版本的jquery在IE6,7下的不同表现，采用原生的设置
		form[0].submit();
	},


	// 插入内容
	// 在图片上传结束后，跳转的页面执行
	// 具体到小百科是 veiw/default/attachment_uploadimg.htm
	insert : function( img_src, img_link ) {
		var self = E.plugin(HdImage),
			data = {},
			Reference = E.plugin('Reference');

		// 获取插入图片面板上参考资料数据
		var panel = self.panel;
		data.doc_title = $('#_doc_title').val();
		data.nodeName = $.trim(panel.find('input[name=site]').val());
		data.title = $.trim(panel.find('input[name=title]').val());
		data.title = data.title == '请注明可靠来源供读者查证' ? '' : data.title;
		data.url = $.trim(panel.find('input[name=url]').val()) =='http://' ? '' : $.trim(panel.find('input[name=url]').val());

		// 补充获取数据的代码...
		if( data.title ){
			Reference.save(data, function(parms){
				self.loadImage(img_src, img_link, parms);
				//同步数据源
				if( parms ){
					parms.title = data.title ;
					parms.url = data.url ;
					parms.site = data.nodeName ;
					parms.nodeName = data.nodeName ;
					Reference.addOne (parms);
				}
			});
		}else{
			self.loadImage(img_src, img_link);
		}
	},

	// 加载图片
	loadImage : function( img_src, img_link, refer ) {
		var self = E.plugin(HdImage),
			panel = self.panel,
			staticAttr = self.staticAttr,
			img = new Image(),
			new_src = img_src,
			imgfig = panel.find("[name=" + staticAttr.imgSizeName+"]").attr('imgsize');
		new_src = img_src.replace(/_140|_s|_950/i, imgfig);
		self.unlock();
		img.onload = function() {
			var title, align, width=parseInt(img.width), imgw=0;
			title = panel.find("[name=" + staticAttr.imgDiscription + "]").val();
			if (!title || title.length <1) {
				if($('#art_title').length){
					title = $('#art_title').val();
				}else if($('#_doc_title').length){
					title = $('#_doc_title').val();
				}else if($('#title').length){
					title = $('#title').val();
				}
			}
			if(width > self.config.max_width ) {
				imgw = self.config.max_width;
			}else {
				imgw = width;
			}
			align = panel.find("[name=" + staticAttr.imgAlignName + "]").val();
			var html = '';
			var style = "width:" + imgw + "px;";
				if (align == 'left') {
					align = 'img_l';
				}else if(align == 'right') {
					align = 'img_r';
				}else {
					align = 'img_c';
					style = "width:" + imgw + "px;";
				}
				html = '<DIV class="img ' + align + '" style="' + style + '">';
				if (img_link) {
					html += '<A title="' + title + '" href="' + img_link + '" target=_blank>';
				}
				html += '<IMG title="' + title + '" alt="' + title + '"';
				//图片大小超过最大值max_width则给img标签加width属性
				if(width > self.config.max_width) {
					html += ' width="' + imgw + '"';
				}
				html += ' src="' + new_src + '"/>';
				if(img_link) {
					html += '</A>';
				}
				if(refer){
					title += '<sup class="refer" name="'+refer.id+'">['+refer.index+']</sup>'
				}
				html += '<STRONG>' + title + '</STRONG>' + '</DIV>';
			self.pasteHTML(html);
			self.hide();
			E.monitor(self.name);
			img.onload = function(){};
			staticAttr.uploadIsBusy = false;

			// ie下在段首插入图片时，会导致一个空p标签
			// 将其去除 2012-11-27 潘雪鹏
			if(Consts.IE){
				setTimeout(function(){
					var emptyP = E.$("img[src='"+new_src+"']").closest("div.img").prev("p");
					if( emptyP.text() == "" ){
						emptyP.remove();
					}
				}, 0);
			}

			E.plugin("DragImage").reset();
		};
		img.onerror = function() {
			self.error('提示：由于网络等原因导致图片插入失败！');
			staticAttr.uploadIsBusy = false;
		}
		img.src = new_src;
	},
	//obj表单数据校验
	_checkFile : function( obj ) {
		if(obj.val().length == 0) {
			return false;
		}
		if(! /\.(jpg|jpeg|gif|png)$/i.test(obj.val())) {
			return false;
		}
		return true;
	},

	resizeImageDivWidth: function(){
		resizeImageDivWidth();
	}
});

})(jQEditor);